<template>
<vs-modal :show.sync="show" effect="fade" width="800px">
  <div class="modal-header" slot="modal-header">
    <h4 class="modal-title">{{$t('common.detail')}}</h4>
  </div>  
  <div class="modal-body" slot="modal-body">
    <validator name="form">
      <form class="form-horizontal">
        <div class="form-group">
          <label class="col-sm-3 control-label">{{$t('list.ipaddr')}}</label>
          <div class="col-sm-8">
            <input type="text" class="form-control" v-model="cfg.ipaddr" v-validate:ipaddr="{ip: true}">
            <strong class="text-danger my-error" v-if="$form.ipaddr.ip">{{$t('error.invalidAddr')}}</strong>
          </div>
        </div>
        <div class="form-group">
          <label class="col-sm-3 control-label">{{$t('list.mask')}}</label>
          <div class="col-sm-8">
            <input type="text" class="form-control" v-model="cfg.mask" v-validate:netmask="{ip: true}">
            <strong class="text-danger my-error" v-if="$form.netmask.ip">{{$t('error.invalidAddr')}}</strong>
          </div>
        </div>
        <div class="form-group">
          <label class="col-sm-3 control-label">{{$t('list.gateway')}}</label>
          <div class="col-sm-8">
            <input type="text" class="form-control" v-model="cfg.gateway" v-validate:gateway="{ip: true}">
            <strong class="text-danger my-error" v-if="$form.gateway.ip">{{$t('error.invalidAddr')}}</strong>
          </div>
        </div>
      </form>
    </validator>
  </div>
  <div class="modal-footer" slot="modal-footer">
    <button type="button" class="btn btn-default" @click.prevent="onQuit">{{$t('common.quit')}}</button>
    <button type="button" class="btn btn-primary" @click.prevent="onSaveChanges">{{$t('common.savechanges')}}</button>
  </div>    
</vs-modal>  
</template>

<script>
import _ from 'lodash'
import ebus from 'libs/ebus.js'
import toast from 'libs/toast.js'
import locales from '../locales/ddisDevList.js'
import {printObj, copyObj} from 'libs/utils.js'
import * as actions from 'share/server/vuex/actions/devmng.js'
import {devmngApi} from 'share/server/api.js'

export default {
  locales,
  vuex: {
    actions
  },
  data () {
    return {
      show: false,
      cfg: {
        devSN: '',
        ipaddr: '',
        mask: '',
        gateway: ''   
      }
    }
  },
  methods: {
    onQuit () {
      this.show = false
    },
    onSaveChanges () {
      if (this.$form.invalid) {
        toast.w(this, this.$t('common.fail'), this.$t('error.params'))
        return
      }

      devmngApi.sendCmd('ifconfig', {
        devSN: this.cfg.devSN,
        ifname: 'eth0',
        dhcp: 0,
        ipaddr: this.cfg.ipaddr,
        mask: this.cfg.mask,
        gateway: this.cfg.gateway
      }).then(function(res) {
        if (res && res.data && res.data.code === 0) {
          toast.s(this, this.$t('common.success'), this.$t('msg.sendCmdSuccess'))
        } else {
          toast.code(this, res.data)
        }
      }.bind(this))
      this.show = false
    },
    onShow (val) {
      copyObj(this.cfg, val)
      this.show = true
    }
  },
  ready () {
    ebus.on('show-ddis-dev-detail', this.onShow)
  }
}
</script>

<style>
</style>